package jjn.round1;

/**
 * @author Jjn
 * @since 2022/9/3 08:13
 */
public class LeetCode34_FindFirstAndLastElementInSortedArray {
    public int[] searchRange(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        int firstIndex = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else {
                firstIndex = mid;
                break;
            }
        }
        if (firstIndex == -1) {
            return new int[]{-1, -1};
        }
        int max = firstIndex, min = firstIndex;
        while (max <= right && nums[max] == target) {
            max++;
        }
        while (min >= left && nums[min] == target) {
            min--;
        }
        return new int[]{min + 1, max - 1};
    }
}
